import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:images_picker/images_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:scan/scan.dart';
import 'package:sptapp/moudle/message.dart';
import 'package:sptapp/widget/common_widget.dart';


class MobileScanPage extends StatefulWidget {
  const MobileScanPage({super.key});

  @override
  State<MobileScanPage> createState() => _MobileScanPageState();
}

class _MobileScanPageState extends State<MobileScanPage> {

  IconData lightIcon = Icons.flash_on;
  final ScanController _controller = ScanController();
  String? sn;

  bool isCamrea = false;
  bool isPhoto = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // _getCamreaStae();
    // _getPhotoStae();
  }

  // Future<bool> _getCamreaStae()async{
  //   bool isCamrea = false;
  //   var status = await Permission.camera.request();
  //   if(status == PermissionStatus.granted){
  //     isCamrea = true;
  //   }
  //   print('权限======== $isCamrea');
  //   if(mounted){
  //     setState(() {});
  //   }
  //   return isCamrea;
  // }

  // Future<bool> _getPhotoStae()async{
  //   if(Platform.isAndroid){
  //     var status = await Permission.storage.request();
  //     if(status == PermissionStatus.granted || status == PermissionStatus.limited){
  //       isPhoto = true;
  //     }
  //     return isPhoto;
  //   }
  //   var status = await Permission.photos.request();
  //   if(status == PermissionStatus.granted || status == PermissionStatus.limited){
  //     isPhoto = true;
  //   }
  //   return isPhoto;
  // }

  void getResult(String result, BuildContext ctx){
    //TODO
    print('结果===='+result);
    if(result!=null&&result.isNotEmpty){
      sn = result;
      Navigator.of(context).pushNamed('MinAddDeviceInput',arguments: {
        'sn':sn
      }).then((val){
        _controller.resume();
      });
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: myAppBar(
          context, add_device_page_scan_btn.tr, backgroundColor: Colors.black,
          isColor: true,
          actions: [
            GestureDetector(
              onTap: () {
                Navigator.of(context).pushNamed('MinAddDeviceInput');
              },
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 40.w, vertical: 10.w),
                child: Text('Manual Entry'.tr, style: TextStyle(
                    color: Color(0xff3c55f8)
                ),),
              ),
            )
          ]),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 80.w),
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(height: 280.h,),
              Container(
                height: 0.8.sw,
                width: 0.8.sw,
                alignment: Alignment.center,
                child: Container(
                  width: 0.8.sw,
                  height: 0.8.sw,
                  child: ScanView(
                    controller: _controller,
                    scanLineColor: Colors.white,
                    onCapture: (data) {
                      _controller.pause();
                      getResult(data, context);
                    },
                    scanAreaScale: 0.9,
                  ),
                ),
              ),
              SizedBox(height: 40.h,),
              Container(
                child: Text(
                  'Align the viewfinder frame with the QR code to automatically scan it'.tr,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp
                  ),textAlign: TextAlign.center,),
              ),
              Expanded(child: Container()),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 60.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                      onTap: (){
                        _controller.toggleTorchMode();
                        if (lightIcon == Icons.flash_on) {
                          lightIcon = Icons.flash_off;
                        } else {
                          lightIcon = Icons.flash_on;
                        }
                        setState(() {});
                      },
                      child: Container(
                        child: Column(
                          children: [
                            Image.asset('assets/home/scan_flash.png',width: 64.w,),
                            Text('Flashlight'.tr,style: TextStyle(
                                color: Colors.white,
                                fontSize: 28.sp
                            )),
                          ],
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTap: ()async{
                        List<Media>? res = await ImagesPicker.pick(
                            count: 1,
                            maxSize: 1024
                        );
                        if (res != null) {
                          _controller.pause();
                          Media image = res.first;
                          String? result = await Scan.parse(image.path);
                          if (result != null) {
                            getResult(result, context);
                          }
                        }
                      },
                      child: Container(
                        child: Column(
                          children: [
                            Image.asset('assets/home/scan_photo.png',width: 64.w,),
                            Text('Photograph'.tr,style: TextStyle(
                                color: Colors.white,
                                fontSize: 28.sp
                            ),),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(height: ScreenUtil().bottomBarHeight+40.h,)
            ]
        ),
      ),
    );
  }
}